﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using RWLibrary.Logic;

namespace RWLibrary.Logic.Graph
{
    public class Node
    {
        public State State
        {
            get;
            private set;
        }

        public bool IsInitial
        {
            get;
            set;
        }

        public string Description { get; set; }
        


        /// <summary>
        /// Tak naprawde Edge zawiera juz cel (w formie state) ale zostawiam Dictionary do kompatybilnosci wstecz
        /// </summary>
        private Dictionary<Edge, Node> _connections = new Dictionary<Edge,Node>();

        /// <summary>
        /// Wszystkie polaczenia z danego Node'a
        /// </summary>
        public Dictionary<Edge, Node> Connections
        {
            get
            {
                return _connections;
            }
        }

        public Node(State state, bool isInitial)
        {
            this.State = state;
            this.IsInitial = isInitial;
        }

        public void addConnection(Edge id, Node destination)
        {
            if(!_connections.Keys.Contains(id))
                _connections.Add(id, destination);
        }

        public override string ToString()
        {
            String result = State.ToString();
            if (IsInitial)
                result += " (Initial)";
            result += " => ";
            foreach(KeyValuePair<Edge,Node> con in _connections)
            {
                result += "[ " + con.Key.Name + " (" + con.Key.Cost + ") " + con.Value.State.ToString() +" ], "; 
            }
            return result;
        }

        public override bool Equals(object obj)
        {
            Node n = obj as Node;
            if (n == null) return false;
            return State.Equals(n.State);
        }

        public override int GetHashCode()
        {
            return State.GetHashCode();
        }
    }
}
